package cn.sumpu.app.guoguojie.dao.impl;

import java.util.List;
import java.util.Map;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.sumpu.app.guoguojie.dao.MemberDetailDao;
import cn.sumpu.app.guoguojie.domain.MemberContact;
import cn.sumpu.app.guoguojie.domain.MemberDetail;
import cn.sumpu.app.guoguojie.domain.MemberLearning;
import cn.sumpu.app.guoguojie.domain.MemberPersonality;
import cn.sumpu.app.guoguojie.domain.MemberWork;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;

public class MemberDetailDaoImpl extends SqlSessionDaoSupport implements
		MemberDetailDao {

	private static Logger logger = LoggerFactory
			.getLogger(MemberDetailDaoImpl.class);

	@Override
	public void insertMemberDetail(MemberDetail detail)
			throws AppBusinessException {
		try {
			getSqlSession()
					.insert("cn.sumpu.app.guoguojie.persistence.MemberDetailMapper.insertMemberDetail",
							detail);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insertMemberDetail failure.", e);
			}
			throw new AppBusinessException("insertMemberDetail failure.", e);
		}
	}

	@Override
	public MemberDetail getMemberDetailByMember(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return (MemberDetail) getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.MemberDetailMapper.getMemberDetailByMember",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getMemberDetailByMember failure.", e);
			}
			throw new AppBusinessException("getMemberDetailByMember failure.",
					e);
		}
	}

	@Override
	public void updateMemberDetail(MemberDetail detail)
			throws AppBusinessException {
		try {
			getSqlSession()
					.update("cn.sumpu.app.guoguojie.persistence.MemberDetailMapper.updateMemberDetail",
							detail);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateMemberDetail failure.", e);
			}
			throw new AppBusinessException("updateMemberDetail failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MemberDetail> getMemberDetailList(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.MemberDetailMapper.getMemberDetailList",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getMemberDetailList failure.", e);
			}
			throw new AppBusinessException("getMemberDetailList failure.", e);
		}
	}

	@Override
	public Integer getMemberDetailListCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			Object obj = getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.MemberDetailMapper.getMemberDetailListCount",
							map);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getMemberDetailListCount failure.", e);
			}
			throw new AppBusinessException("getMemberDetailListCount failure.",
					e);
		}
	}

	// 根据会员id查询个性化资料
	@SuppressWarnings("unchecked")
	@Override
	public List<MemberPersonality> getMemberPersonalityList(
			Map<String, Object> map) throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.MemberDetailMapper.getMemberPersonalityList",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Get member personality list failure.", e);
			}
			throw new AppBusinessException(
					"Get member personality list failure.", e);
		}
	}

	// 根据会员id查询工作资料
	@SuppressWarnings("unchecked")
	@Override
	public List<MemberWork> getMemberWorkList(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.MemberDetailMapper.getMemberWorkList",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getWorkList failure.", e);
			}
			throw new AppBusinessException("getWorkList failure.", e);
		}
	}

	// 根据会员id查询学习资料
	@SuppressWarnings("unchecked")
	@Override
	public List<MemberLearning> getMemberLearningList(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.MemberDetailMapper.getMemberLearningList",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getLearningList failure.", e);
			}
			throw new AppBusinessException("getLearningList failure.", e);
		}
	}

	@Override
	public void deleteMemberPersonality(Map<String, Object> map)
			throws AppBusinessException {
		try {
			getSqlSession()
					.delete("cn.sumpu.app.guoguojie.persistence.MemberDetailMapper.deleteMemberPersonality",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Delete member personality failure.", e);
			}
			throw new AppBusinessException(
					"Delete member personality failure.", e);
		}
	}

	@Override
	public void insertMemberPersonality(MemberPersonality memberPersonality)
			throws AppBusinessException {
		try {
			getSqlSession()
					.delete("cn.sumpu.app.guoguojie.persistence.MemberDetailMapper.insertMemberPersonality",
							memberPersonality);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Insert member personality failure.", e);
			}
			throw new AppBusinessException(
					"Insert member personality failure.", e);
		}
	}

	@Override
	public void updateMemberPersonality(MemberPersonality memberPersonality)
			throws AppBusinessException {
		try {
			getSqlSession()
					.delete("cn.sumpu.app.guoguojie.persistence.MemberDetailMapper.updateMemberPersonality",
							memberPersonality);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Update member personality failure.", e);
			}
			throw new AppBusinessException(
					"Update member personality failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MemberContact> getMemberContactList(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.MemberDetailMapper.getMemberContactList",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Get member contact list failure.", e);
			}
			throw new AppBusinessException("Get member contact list failure.",
					e);
		}
	}

	@Override
	public void deleteMemberContact(Map<String, Object> map)
			throws AppBusinessException {
		try {
			getSqlSession()
					.delete("cn.sumpu.app.guoguojie.persistence.MemberDetailMapper.deleteMemberContact",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Delete member contact failure.", e);
			}
			throw new AppBusinessException("Delete member contact failure.", e);
		}
	}

	@Override
	public void insertMemberContact(MemberContact memberContact)
			throws AppBusinessException {
		try {
			getSqlSession()
					.delete("cn.sumpu.app.guoguojie.persistence.MemberDetailMapper.insertMemberContact",
							memberContact);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Insert member contact failure.", e);
			}
			throw new AppBusinessException("Insert member contact failure.", e);
		}
	}

}
